//
//
// *  $Id: TestPerformance.java,v 1.4 2003/09/21 15:49:02 boisvert Exp $
//
// *
//
// *  Package performance test
//
// *
//
// *  Simple db toolkit
//
// *  Copyright (C) 1999, 2000 Cees de Groot <cg@cdegroot.com>
//
// *
//
// *  This library is free software; you can redistribute it and/or
//
// *  modify it under the terms of the GNU Library General Public License
//
// *  as published by the Free Software Foundation; either version 2
//
// *  of the License, or (at your option) any later version.
//
// *
//
// *  This library is distributed in the hope that it will be useful,
//
// *  but WITHOUT ANY WARRANTY; without even the implied warranty of
//
// *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//
// *  Library General Public License for more details.
//
// *
//
// *  You should have received a copy of the GNU Library General Public License
//
// *  along with this library; if not, write to the Free Software Foundation,
//
// *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
//
// 


//*************************************************************************
//**  Included in JDMB 1.0 port to SharpDBM;  11/2013 Cyrus Neah cneah@codingreal.com
//**  SharpDBM is an independent reimplementation of the JDBM 1.0 software library  
//*************************************************************************

using System;
using SharpDBM;
using SharpDBM.recman;
using SharpDBM.helper;
using SharpDBM.btree;
using NUnit.Framework;



namespace SharpDBM.Test

.recman
{



	using RecordManager = SharpDBM.RecordManager;

	using RecordManagerFactory = SharpDBM.RecordManagerFactory;

    using RecordManagerOptions = SharpDBM.RecordManager;



	using NUnit.Framework;





///
//
// *  This class contains performance tests for this package.
//
// 

	[TestFixture]
    public class TestPerformance  
	{



	



		[Test]
 public virtual void setUp()
		{

			TestRecordFile.deleteTestFile();

		}



		[Test]
 public virtual void tearDown()
		{

		//TestRecordFile.deleteTestFile();

		}



	// test parameter: maximum record size

		internal readonly int MAXSIZE = 500; // is this a reasonable size for real-world apps?

	// test parameter: number of records for fetch/update tests

		internal readonly int RECORDS = 10;



		internal Random rnd = new Random(42);



//    *
//
//     *  Test insert performance
//
//     

 
//
		[Test]
 public virtual void testInserts()
		{

			RecordManager recman;



			recman = RecordManagerFactory.createRecordManager(TestRecordFile.testFileName);



			int inserts = 0;

			long Start = System.DateTime.Now.Millisecond;

			try
			{



				long Stop = 0;

				while (true)
				{



					recman.insert(TestUtil.makeRecord(rnd.Next(MAXSIZE), (sbyte) rnd.Next()));

					inserts++;

					if ((inserts % 25) == 0)
					{

						Stop = System.DateTime.Now.Millisecond;

						

							break;

					}

				}

				recman.close();

				Console.WriteLine("Inserts: " + inserts + " in " + (Stop - Start) + " millisecs");

					}
			catch (System.Exception e)
			{

				NUnit.Framework.Assert.Fail("unexpected exception after " + inserts + " inserts, " + (System.DateTime.Now.Millisecond - Start) + "ms: " + e);

			}

		}



//    *
//
//     *  Create a database, return array of rowids.
//
//     

 
//
		private long[] makeRows()
		{

			RecordManager recman;

			System.Configuration.ConfigurationManager.AppSettings["DISABLE_TRANSACTIONS"]="true";


			recman = RecordManagerFactory.createRecordManager(TestRecordFile.testFileName);



            long[] retval = new long[RECORDS];

			Console.Write("Creating test database");

			long Start = System.DateTime.Now.Millisecond;

			try
			{

				for (int i = 0; i < RECORDS; i++)
				{

					retval[i] = recman.insert(TestUtil.makeRecord(rnd.Next(MAXSIZE), (sbyte) rnd.Next()));

					if ((i % 100) == 0)

						Console.Write(".");

				}

				recman.close();

				}
			catch (System.Exception e)
			{

				Console.WriteLine(e.StackTrace);

				NUnit.Framework.Assert.Fail("unexpected exception during db creation: " + e);

			}



			Console.WriteLine("done (" + RECORDS + " inserts in " + (System.DateTime.Now.Millisecond - Start) + "ms).");

			return retval;

		}



//    *
//
//     *  Test fetches
//
//     

 
//
		[Test]
 public virtual void testFetches()
		{

			RecordManager recman;



			long[] rowids = makeRows();



			recman = RecordManagerFactory.createRecordManager(TestRecordFile.testFileName);



			int fetches = 0;

			long Start = System.DateTime.Now.Millisecond;
            long Stop = 0;
			try
			{



				

				while (true)
				{

					recman.fetch(rowids[rnd.Next(RECORDS)]);

					fetches++;

					if ((fetches % 25) == 0)
					{

                       
                        Stop = System.DateTime.Now.Millisecond;
                        break;
					}

				}

				recman.close();

				Console.WriteLine("Fetches: " + fetches + " in " + (Stop - Start) + " millisecs");

					}
			catch (System.Exception e)
			{

				NUnit.Framework.Assert.Fail("unexpected exception after " + fetches + " fetches, " + (System.DateTime.Now.Millisecond - Start) + "ms: " + e);

			}

		}



//    *
//
//     *  Test updates.
//
//     

 
//
		[Test]
 public virtual void testUpdates()
		{

			RecordManager recman;



			long[] rowids = makeRows();



			recman = RecordManagerFactory.createRecordManager(TestRecordFile.testFileName);



			int updates = 0;

            long Start = System.DateTime.Now.Millisecond;

			try
			{



				long Stop = 0;

				while (true)
				{



					recman.update(rowids[rnd.Next(RECORDS)], TestUtil.makeRecord(rnd.Next(MAXSIZE), (sbyte) rnd.Next()));

					updates++;

					if ((updates % 25) == 0)
					{

						Stop = System.DateTime.Now.Millisecond;

                        break;

					}

				}

				recman.close();

				Console.WriteLine("Updates: " + updates + " in " + (Stop - Start) + " millisecs");

					}
			catch (System.Exception e)
			{

				NUnit.Framework.Assert.Fail("unexpected exception after " + updates + " updates, " + (System.DateTime.Now.Millisecond - Start) + "ms: " + e);

			}

		}



//    *
//
//     *  Test deletes.
//
//     

 
//
		[Test]
 public virtual void testDeletes()
		{

			RecordManager recman;



			long[] rowids = makeRows();



			recman = RecordManagerFactory.createRecordManager(TestRecordFile.testFileName);



			int deletes = 0;

			long Start = System.DateTime.Now.Millisecond;

			try
			{



				long Stop = 0;

			// This can be done better...

				for (int i = 0; i < RECORDS; i++)
				{

					recman.delete(rowids[i]);

					deletes++;

					if ((deletes % 25) == 0)
					{

						Stop = System.DateTime.Now.Millisecond;

						//if (Stop - Start >= 60000)

							break;

					}

				}

				recman.close();

				Console.WriteLine("Deletes: " + deletes + " in " + (Stop - Start) + " millisecs");

					}
			catch (System.Exception e)
			{

				Console.WriteLine(e.StackTrace);

				NUnit.Framework.Assert.Fail("unexpected exception after " + deletes + " deletes, " + (System.DateTime.Now.Millisecond - Start) + "ms: " + e);

			}

		}



//    *
//
//     *  Runs all tests in this class
//
//     

		static void Main(string[] args)
		{

			// junit.textui.TestRunner.run(new TestSuite(typeof(TestPerformance)));



		// if you just want one test:

		//  TestSuite suite = new TestSuite();

		//  suite.addTest(new TestPerformance("testDeletes"));

		//  // junit.textui.TestRunner.run(suite);

		}

	}


}